<?php

namespace app\admin\model;

use think\Model;

class PurchaseOrders extends BaseModel
{
    protected $attributes = [
        'pay_type' => [
            '1' => 'TRC20:USDT',
            '2' => 'ERC20:USDT',
            '3' => 'ERC20:USDC',
            '4' => '人民币-支付宝',
            '5' => '人民币-微信',
            '6' => '人民币-对私',
            '7' => '人民币-对公',
            '8' => 'USD-香港账户',
            '9' => 'EUR-香港账户'
        ],
        'pay_terms' => [
            1 => '预付款',
            0 => '全款'
        ],
        'pay_status' => [
            0 => '未支付',
            1 => '部份支付',
            2 => '已支付'
        ],
        'status' => [
            // 10 => '待审核',
            20 => '待支付',
            30 => '待入库',
            40 => '已完成',
            // 50 => '取消',
            // 60 => '部分退货',
            // 70 => '退货'
        ],
        'type' => [
            1 => '主件工厂直采',
            2 => '主件提篮子',
            3 => '配件'
        ]
    ];

    protected $enumCallbacks = [
        'currency' => 'getCurrencyEnum'
    ];

    public function getCurrencyEnum()
    {
        return TransactionCurrency::where('is_del', 0)->where('status', 1)->column('id AS value,name as label');
    }

    public function getFieldEnum($field)
    {
        return $this->getEnum($field, []);
    }

    public function purchaseGoods()
    {
        return $this->hasMany(PurchaseOrdersDetail::class, 'orderid', 'id')
            ->field(['orderid', 'productid', 'num', 'unit_price', 'notes', 'id as detail_id', 'order_id'])
            ->with(['product', 'receipt', 'order'])
            ->append(['entry_num']);
    }

    public function getPayTypeAttr($value, $data)
    {
        return intval($value);
    }

    public function getPayTermsAttr($value, $data)
    {
        return intval($value);
    }

    public function getSignatoryidAttr($value, $data)
    {
        return intval($value);
    }

    public function getOwnerAttr($value, $data)
    {
        return intval($value);
    }

    //    public function getPartnerAttr($value, $data)
    //    {
    //        return intval($value);
    //    }

    public function getSupidValue()
    {
        return $this->belongsTo(SellerInfo::class, 'supid', 'id')->bind(['supid_value' => 'name']);
    }

    public function getSignatoryidValue()
    {
        return $this->belongsTo(Operator::class, 'signatoryid', 'id')->bind(['signatoryid_value' => 'cname']);
    }

    public function getOwnerValue()
    {
        return $this->belongsTo(Operator::class, 'owner', 'id')->bind(['owner_value' => 'cname']);
    }

    public function getPartnerValue()
    {
        return $this->belongsTo(Operator::class, 'partner', 'id')->bind(['partner_value' => 'cname']);
    }

    public function getStatusValueAttr($value, $data)
    {
        return isset($this->attributes['status'][$data['status']]) ? $this->attributes['status'][$data['status']] : '';
    }

    public function getPayTypeValueAttr($value, $data)
    {
        return isset($this->attributes['pay_type'][$data['pay_type']]) ? $this->attributes['pay_type'][$data['pay_type']] : '';
    }

    public function getPayTermsValueAttr($value, $data)
    {
        return isset($this->attributes['pay_terms'][$data['pay_terms']]) ? $this->attributes['pay_terms'][$data['pay_terms']] : '';
    }

    /**
     * 关联付款单
     * @return \think\model\relation\HasMany
     */
    public function payments()
    {
        return $this->hasMany(PurchasePaymentOrder::class, 'purchaseid', 'id');
    }

    public function partners()
    {
        $partnerIds = explode(',', $this->partner);
        return Operator::whereIn('id', $partnerIds);
    }

    public function itemList()
    {
        return $this->hasMany(PurchaseOrdersDetail::class, 'orderid', 'id')
            ->field(['orderid', 'productid', 'num', 'unit_price', 'notes', 'id as detail_id', 'order_id'])
            ->with(['product', 'order']);
    }
}
